import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:flutter_juwo/common/common_index.dart';
import 'package:flutter_juwo/ui/custom/noData.dart';
import 'package:flutter_juwo/ui/model/message_detail_entity.dart';
import 'package:flutter_juwo/ui/utils/toast_utils.dart';

class MessageDetails extends StatefulWidget {
  final String id;

  MessageDetails(this.id);

  @override
  _MessageDetailsState createState() => _MessageDetailsState();
}

class _MessageDetailsState extends State<MessageDetails> {
  MyPageState ps = MyPageState.LOADING;
  MessageDetailEntity entity = null;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    print("获取到id：${widget.id}");
    this.initData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: UiUtils.appBar(context, "通知详情"),
      body: this.getContent(),
    );
  }

  void initData() {
    getData();
  }

  Future getData() async {
    FormData formData = FormData.from({"id": widget.id});
    DioManager.getInstance().get(Api.MESSAGE_DETAIL, formData, (json) {
      print("获取详情：${json}");
      entity = MessageDetailEntity.fromJson(json);
      if (null == entity) {
        ///
        ps = MyPageState.NOT_DATA;
        setState(() {});
        return;
      }
      if (entity.code == Api.ERROR_CODE) {
        ToastUtil.showShortToast(entity.message);
        return;
      }

      if (entity.code == Api.SUCCESS_CODE) {
        ///
        if (null == entity.data) {
          ps = MyPageState.NOT_DATA;
          setState(() {});
          return;
        }

        ps = MyPageState.HAVE_DATA;
        setState(() {});
        return;
      }
    }, (errMsg) {
      print("获取详情ERR：${errMsg}");
    });
  }

  getContent() {
    if (ps == MyPageState.LOADING) {
      return LoadingPage();
    }

    if (ps == MyPageState.NOT_DATA) {
      return NoData(this.getData);
    }

    if (ps == MyPageState.HAVE_DATA) {
      return Container(
        child: EasyRefresh(
            child: ListView(
          children: <Widget>[
            Container(
              color: Colors.white,
              padding: EdgeInsets.all(ScreenUtil().setWidth(30)),
              child: Text(
                "${entity.data.remark}",
                style: TextStyles.custom(
                    32, Colours.appC2, Font.PingFangMedium),
              ),
            ),
            UiUtils.divider(),
            Container(
              color: Colors.white,
              padding: EdgeInsets.all(ScreenUtil().setWidth(30)),
              child: Text(
                "${entity.data.sendTime}",
                style: TextStyles.custom(
                    28, Colours.appC3, Font.PingFangRegular),
              ),
            ),
            Container(
              color: Colors.white,
              padding: EdgeInsets.fromLTRB(
                  ScreenUtil().setWidth(60),
                  ScreenUtil().setWidth(30),
                  ScreenUtil().setWidth(30),
                  ScreenUtil().setWidth(30)),
              child:  Html(
              data: "${entity.data.content}",

                blockSpacing: 0,
              ),
            ),
            Container(
              alignment: Alignment.bottomRight,
              color: Colors.white,
              padding: EdgeInsets.fromLTRB(
                  ScreenUtil().setWidth(60),
                  ScreenUtil().setWidth(30),
                  ScreenUtil().setWidth(30),
                  ScreenUtil().setWidth(30)),
              child: Text(
                "平台管理端",
                style: TextStyles.custom(
                    28, Colours.appC3, Font.PingFangRegular),
              ),
            )
          ],
        )),
      );
    }
  }
}
